
/**
 * 初始化
 */
~function () {

    document.getElementById('box_btn').addEventListener('click', function (ev) {
        let btn = ev.target.getAttribute('data-ev');
        events[btn] && events[btn]();
    });
}();

/**
 * 页面
 */
const page = {
    // 把书签树整合到一个数组返回
    treeToArray(tree, arr = []) {
        for (const item of tree) {
            if (item.url) arr.push(item);
            if (item.children) this.treeToArray(item.children, arr);
        }
        return arr;
    },
    // 书签数组重复项
    repetition(arr) {
        let ary = [];

        for (let i = 0; i < arr.length; i++) {
            let cur = arr[i];
            let tmp = [cur];
            let cur_url = util.getURl(cur.url);
            for (let j = i + 1; j < arr.length; j++) {
                let arr_url = util.getURl(arr[j].url);
                if (cur_url == arr_url) {
                    tmp.push(arr[j]);
                    arr.splice(j, 1);
                    j--;
                }
            }
            if (tmp.length > 1) {
                ary.push(tmp);
            }
        }
        return ary;
    },
    // 把重复项add to page
    repetitionToPage(arr) {
        let f = document.createDocumentFragment();

        for (const item of arr) {
            let li = document.createElement('li');
            let url = document.createElement('a');
            // url.target 
            li.appendChild(url);
            for (const val of item) {
                let p = document.createElement('p');
                let del = document.createElement('a');
                let folder = document.createElement('span');
                let title = document.createElement('span');

                title.innerHTML = val.title;

                del.innerHTML = 'Del';
                del.href = '#';
                del.setAttribute('data-id', val.id);
                del.onclick = events.removeBookmark;

                bookmark.getBookmark(val.parentId).then((bookmark) => {
                    folder.innerHTML = bookmark.title + "：";
                });

                url.href = val.url;
                url.innerHTML = val.url;

                p.appendChild(del);
                p.appendChild(folder);
                p.appendChild(title);

                li.appendChild(p);
            }
            f.appendChild(li);
        }

        if (util.childrens.call(f).length) {
            document.getElementById('box_repetition').appendChild(f);
        } else {
            alert('没有发现重复书签');
        }
        f = null;

        // <li>
        //     <a>url</a>
        //     <p>
        //         <a href="javascript:;">删除</a>
        //         <span>文件夹：</span>:
        //         <span>百度</span>
        //     </p>
        //     <p>
        //         <a href="javascript:;">删除</a>
        //         <span>文件夹</span>:
        //         <span>百度</span>
        //     </p>
        // </li>
    },
    // 移除当前元素所属的li
    removeLi(ele) {

        let li = util.parents.call(ele, 'li');

        // 1判断li下还有多少p元素
        if (util.childrens.call(li, 'p').length <= 2) {
            // 删除li
            util.remove.call(li)
        } else {
            // 删除p
            let p = util.parents.call(ele, 'p');
            util.remove.call(p);
        }
    },
    // 合并重复文件夹
    delRepetition(tree) {
        
    }
}
/**
 * 工具
 */
const util = {
    // 获取父节点
    parent() {
        return this.parentNode;
    },
    // 获取父节点，根据tagName
    parents(tagName) {
        let oParent = util.parent.call(this);
        while (true) {
            if (!tagName || oParent.nodeName === tagName.toUpperCase()) {
                return oParent;
            } else if (oParent.nodeName === 'BODY') {
                return;
            } else {
                oParent = util.parent.call(oParent);
            }
        }
    },
    // 获取标签的子元素
    childrens(tagName) {
        let arr = []

        for (const node of this.children) {
            if (!tagName || node.nodeName === tagName.toUpperCase()) {
                arr.push(node);
            }
        }
        return arr;
    },
    // 移除元素
    remove() {
        let p = util.parent.call(this);
        p.removeChild(this);
    },
    // 获取url的去掉协议部分
    getURl(url) {
        let reg = /^(https?\:\/\/)?(.+)$/ig;
        return reg.exec(url)[2];
    }
}
